package com.supcon.exchangedata.service.hj;

import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoArea;
import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoAreaDetail;
import com.supcon.exchangedata.pojo.entity.oracle.GisPoint;
import com.supcon.exchangedata.pojo.entity.mysql.operation.hj.HjCleanDisposeStation;
import com.supcon.exchangedata.repository.master.MasterBaseAreaRepository;
import com.supcon.exchangedata.repository.master.MasterBaseGeoAreaDetailRepository;
import com.supcon.exchangedata.repository.master.MasterGisPointRepository;
import com.supcon.exchangedata.repository.slave6.SlaveHjBasicInfo2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 处置场信息
 *
 * @Author: yangbin
 * @Date: 2022/03/17/15:59
 * @Description:
 */
@Service
@Slf4j
public class GarbageDisposalSiteService {

    @Autowired
    private MasterBaseAreaRepository masterBaseAreaRepository;
    @Autowired
    private MasterGisPointRepository masterGisPointRepository;

    @Resource
    private MasterBaseGeoAreaDetailRepository masterBaseGeoAreaDetailRepository;

    @Resource
    private SlaveHjBasicInfo2 slaveHjBasicInfo2;


    /**
     * 垃圾处置场信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void transGarbageDisposalSite() {
        List<BaseGeoAreaDetail> removeLists = new ArrayList<>();
        List<BaseGeoArea> baseGeoAreasByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(4016L);
        if (!CollectionUtils.isEmpty(baseGeoAreasByAreaType)) {
            for (BaseGeoArea baseGeoAreaDetail : baseGeoAreasByAreaType) {
                BaseGeoAreaDetail baseGeoAreaDetail1 = new BaseGeoAreaDetail();
                baseGeoAreaDetail1.setAreaId(baseGeoAreaDetail.getId());
                removeLists.add(baseGeoAreaDetail1);
            }
            masterBaseGeoAreaDetailRepository.deleteAll(removeLists);
            baseGeoAreasByAreaType.clear();
        }
        //查询出应急库内停保基地  1004
        masterBaseAreaRepository.deleteBaseGeoAreaByAreaType(4016L);
        masterGisPointRepository.deleteGisPointByBusinessCategory("4016");

        List<HjCleanDisposeStation> all = slaveHjBasicInfo2.findAll();

        List<BaseGeoArea> removeList = new ArrayList<>();
        List<BaseGeoArea> updateList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(baseGeoAreasByAreaType)) {
            baseGeoAreasByAreaType.forEach((e) -> {

                if (!CollectionUtils.isEmpty(all)) {
                    Iterator<HjCleanDisposeStation> iterator = all.iterator();
                    boolean removeFlag = true;
                    while (iterator.hasNext()) {
                        HjCleanDisposeStation next = iterator.next();

                        //筛选测试数据
                        if (next.getCleanStationName().equals(e.getAreaName())) {
                            BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();

                            removeFlag = false;
                            e.setAreaCode("hjljczca" + next.getId());
                            e.setUpdateDate(LocalDateTime.now());

                            //地址
                            e.setAddress(next.getCleanStationAddress());
                            //联系人电话
                            //  e.setContactsPhone(next.getCleanStationUser());
                            //联系人
                            e.setContacts(next.getCleanStationUser());

                            //日处理能力
                            baseGeoAreaDetail.setSubsidiaryInformation1(next.getCleanStationCapacity());

                            e.setBaseGeoAreaDetail(baseGeoAreaDetail);
                            if (!StringUtils.isEmpty(next.getCleanStationLatitude())) {
                                e.setLatitude(Double.valueOf(next.getCleanStationLatitude()));

                            }
                            e.setAddress(next.getCleanStationAddress());
                            if (!StringUtils.isEmpty(next.getCleanStationLongitude())) {
                                Double aLong = Double.valueOf(next.getCleanStationLongitude());
                                e.setLongitude(aLong);
                            }
                            updateList.add(e);
                            iterator.remove();
                            break;
                        }
                    }
                    if (removeFlag) {
                        removeList.add(e);
                    }
                }


            });
        }

        deleteAreaAndPoint(removeList);
        updateGarbageDisposalSite(updateList);
        if (!CollectionUtils.isEmpty(all)) {
            int i = 0;
            for (HjCleanDisposeStation station : all) {
                BaseGeoArea area = new BaseGeoArea();
                i++;
                area.setAreaCode("hjljczca" + station.getId());
                area.setParentId(222833L);
                area.setAreaType(4016L);
                area.setAreaName(station.getCleanStationName());
                area.setFullLevelName("环境→垃圾处置场→" + station.getCleanStationName());
                area.setUpdator("jpa-program");
                area.setUpdateDate(LocalDateTime.now());
                BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                //中转站地址
                area.setAddress(station.getCleanStationAddress());
                //联系人电话
                // area.setContactsPhone(station.getLinkmanPhone());
                //联系人
                area.setContacts(station.getCleanStationUser());
                //日处理能力
                baseGeoAreaDetail.setSubsidiaryInformation1(station.getCleanStationCapacity());
                if (!StringUtils.isEmpty(station.getCleanStationLatitude())) {
                    Double aLong = Double.valueOf(station.getCleanStationLatitude());
                    area.setLatitude(aLong);

                }
                if (!StringUtils.isEmpty(station.getCleanStationLongitude())) {
                    Double aLong = Double.valueOf(station.getCleanStationLongitude());
                    area.setLongitude(aLong);
                }
                BaseGeoArea save = masterBaseAreaRepository.save(area);
                if(area.getLongitude() != null && area.getLatitude() != null){
                    GisPoint gisPoint = new GisPoint();
                    gisPoint.setCode("hjljczca" + station.getId());
                    gisPoint.setObjType(1L);
                    gisPoint.setName(station.getCleanStationName());
                    gisPoint.setObjCode("hjljczca" + station.getId());
                    gisPoint.setBusinessCategory("4016");
                    gisPoint.setRoad(station.getCleanStationAddress());
                    gisPoint.setLatitude(area.getLatitude());
                    gisPoint.setLongitude(area.getLongitude());
                    gisPoint.setObjId(save.getId());
                    masterGisPointRepository.save(gisPoint);
                }

                baseGeoAreaDetail.setAreaId(save.getId());
                masterBaseGeoAreaDetailRepository.save(baseGeoAreaDetail);
            }

        }

    }

    /**
     * 更新垃圾处置场相关图层信息
     *
     * @param updateList
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateGarbageDisposalSite(List<BaseGeoArea> updateList) {
        if (!CollectionUtils.isEmpty(updateList)) {
            updateList.forEach((e) -> {
                e.setParentId(222833L);
                e.setAreaType(4016L);
                e.setUpdator("jpa-program");
                e.setUpdateDate(LocalDateTime.now());
                masterBaseAreaRepository.save(e);
                //更新gisPoint
                if (e.getLongitude() != null) {
                    GisPoint distinctByObjId = masterGisPointRepository.findDistinctByObjId(e.getId());
                    if (distinctByObjId == null) {
                        distinctByObjId = new GisPoint();
                    }
                    distinctByObjId.setObjId(e.getId());
                    distinctByObjId.setObjCode(e.getAreaCode());
                    distinctByObjId.setCode(e.getAreaCode());
                    distinctByObjId.setBusinessCategory(String.valueOf(e.getAreaType()));
                    distinctByObjId.setYwCategory(String.valueOf(e.getAreaType()));
                    distinctByObjId.setLatitude(e.getLatitude());
                    distinctByObjId.setLongitude(e.getLongitude());
                    distinctByObjId.setRoad(e.getAddress());
                    distinctByObjId.setName(e.getAreaName());
                    distinctByObjId.setObjType(1L);
                    masterGisPointRepository.save(distinctByObjId);
                }
                BaseGeoAreaDetail distinctByAreaId = masterBaseGeoAreaDetailRepository.findDistinctByAreaId(e.getId());
                if (distinctByAreaId == null) {
                    distinctByAreaId = new BaseGeoAreaDetail();
                }
                BaseGeoAreaDetail baseGeoAreaDetail = e.getBaseGeoAreaDetail();
                if (baseGeoAreaDetail != null) {
                    //容量
                    distinctByAreaId.setSubsidiaryInformation1(baseGeoAreaDetail.getSubsidiaryInformation1());
                }
                distinctByAreaId.setAreaId(e.getId());
                masterBaseGeoAreaDetailRepository.save(distinctByAreaId);

            });
        }
    }

    /**
     * 删除不需要的图层点位信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteAreaAndPoint(List<BaseGeoArea> removeList) {
        if (!CollectionUtils.isEmpty(removeList)) {
            removeList.forEach((e) -> {
                masterBaseAreaRepository.deleteById(e.getId());
                masterGisPointRepository.deleteByObjIdAndYwCategory(e.getId(), String.valueOf(e.getAreaType()));
                masterBaseGeoAreaDetailRepository.deleteByAreaId(e.getId());
            });
        }
    }
}
